home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 8 / NAMESPAC.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  178 lines

  1. // File from fragments starting on page 340 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: NAMESPAC.CPP -- Namespace example fragments from Chapter 8
  34.  
  35. //: HEADER1.H
  36. namespace MyLib {
  37.   extern int X;
  38.   void f();
  39.   // ...
  40. }
  41.  
  42. //: HEADER2.H
  43. // Add more names to MyLib
  44. namespace MyLib { // NOT a redefinition!
  45.   extern int Y;
  46.   void g();
  47.   // ...
  48. }
  49.  
  50. namespace BobsSuperDuperLibrary {
  51.   class widget { /* ... */ };
  52.   class poppit { /* ... */ };
  53.   // ...
  54. }
  55. // Too much to type! I'll alias it:
  56. namespace Bob = BobsSuperDuperLibrary;
  57.  
  58. namespace {
  59.   class arm  { /* ... */ };
  60.   class leg  { /* ... */ };
  61.   class head { /* ... */ };
  62.   class robot {
  63.     arm Arm[4];
  64.     leg Leg[16];
  65.     head Head[3];
  66.     // ...
  67.   } Xanthan;
  68.   int i, j, k;
  69. }
  70.  
  71. namespace me {
  72.   class us {
  73.     //...
  74.     friend you();
  75.   };
  76. }
  77.  
  78. namespace X {
  79.   class Y {
  80.     static int i;
  81.   public:
  82.     void f();
  83.   };
  84.   class Z;
  85.   void foo();
  86. }
  87.  
  88. int X::Y::i = 9;
  89. class X::Z {
  90.   int u, v, w;
  91. public:
  92.   Z(int I);
  93.   int g();
  94. };
  95.  
  96. X::Z::Z(int I) { u = v = w = I; }
  97. int X::Z::g() { return u = v = w = 0; }
  98. void X::foo() {
  99.   X::Z a(1);
  100.   a.g();
  101. }
  102.  
  103. namespace math {
  104.   enum sign { positive, negative };
  105.   class integer {
  106.     int i;
  107.     sign s;
  108.   public:
  109.     integer(int I = 0) 
  110.       : i(I),
  111.         s(i >= 0 ? positive : negative)
  112.     {}
  113.     sign Sign() { return s; }
  114.     void Sign(sign S) { s = S; }
  115.     // ...
  116.   };
  117.   integer A, B, C;
  118.   integer divide(integer, integer);
  119.   // ...
  120. }
  121.  
  122. void arithmetic() {
  123.   using namespace math;
  124.   integer X;
  125.   X.Sign(positive);
  126. }
  127.  
  128. void f() {
  129.   using namespace math;
  130.   integer A; // Hides math::A;
  131.   A.Sign(negative);
  132.   math::A.Sign(positive);
  133. }
  134.  
  135. namespace calculation {
  136.   class integer {};
  137.   integer divide(integer, integer);
  138.   // ...
  139. }
  140.  
  141. void g() {
  142.   using namespace math;
  143.   using namespace calculation;
  144.   // Everything's ok until:
  145.   divide(1, 2); // Ambiguity
  146. }
  147.  
  148. namespace U {
  149.   void f() {};
  150.   void g() {};
  151. }
  152.  
  153. namespace V {
  154.   void f() {};
  155.   void g() {};
  156. }
  157.  
  158. void func() {
  159.   using namespace U; // Using directive
  160.   using V::f; // Using declaration
  161.   f(); // Calls V::f();
  162.   U::f(); // Must fully qualify to call
  163. }
  164.  
  165. namespace Q {
  166.   using U::f;
  167.   using V::g;
  168.   // ...
  169. }
  170.  
  171. void m() {
  172.   using namespace Q;
  173.   f(); // Calls U::f();
  174.   g(); // Calls V::g();
  175. }
  176.  
  177. main() {}
  178.